{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0958d69a-9847-49e0-a10a-421536113dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[64.58234786987305, 79.6964168548584, 91.51124954223633, 32.72080421447754, 103.00993919372559]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[65.00387191772461, 80.20305633544922, 91.51935577392578, 31.89396858215332, 105.4985523223877]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[62.00456619262695, 79.30326461791992, 91.92872047424316, 31.46839141845703, 105.59988021850586]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[60.23740768432617, 79.6356201171875, 92.38266944885254, 31.89396858215332, 104.30693626403809]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[63.13943862915039, 79.5748233795166, 91.40992164611816, 32.2871208190918, 102.37360000610352]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[67.84915924072266, 79.42891120910645, 92.09084510803223, 32.70459175109863, 102.75864601135254]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[62.0572566986084, 71.93875312805176, 83.55498313903809, 27.078866958618164, 28.590679168701172]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[62.55984306335449, 72.42512702941895, 82.78489112854004, 27.099132537841797, 28.161048889160156]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[66.41435623168945, 64.37969207763672, 73.35329055786133, 23.844480514526367, 25.27928352355957]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[66.45894050598145, 64.41617012023926, 72.98445701599121, 23.763418197631836, 24.825334548950195]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[69.9770450592041, 55.2520751953125, 60.52517890930176, 19.73867416381836, 26.795148849487305]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[69.94462013244629, 55.23180961608887, 60.70756912231445, 19.742727279663086, 20.63441276550293]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[70.80388069152832, 55.58037757873535, 59.386253356933594, 19.72651481628418, 19.434690475463867]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[74.59759712219238, 48.0496883392334, 50.894975662231445, 18.085002899169922, 17.606735229492188]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[73.76670837402344, 47.99294471740723, 51.72991752624512, 18.085002899169922, 17.091989517211914]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[198.80127906799316, 51.23138427734375, 55.77492713928223, 9.147882461547852, 9.755849838256836]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[198.80938529968262, 51.22733116149902, 55.35745620727539, 9.58561897277832, 9.768009185791016]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[200.8967399597168, 51.65696144104004, 55.76276779174805, 9.626150131225586, 9.755849838256836]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[192.81482696533203, 46.18525505065918, 52.309513092041016, 15.584230422973633, 16.050338745117188]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[94.1619873046875, 46.65541648864746, 52.34193801879883, 15.195131301879883, 16.03412628173828]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[120.56398391723633, 38.86127471923828, 42.63472557067871, 12.32147216796875, 12.382268905639648]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[119.76146697998047, 38.350582122802734, 43.8992977142334, 12.325525283813477, 12.337684631347656]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[118.89815330505371, 39.90292549133301, 48.40230941772461, 19.848108291625977, 20.808696746826172]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[80.32870292663574, 39.82996940612793, 48.78735542297363, 18.919944763183594, 20.808696746826172]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[82.40795135498047, 43.04814338684082, 54.631948471069336, 25.688648223876953, 27.553081512451172]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[57.732582092285156, 56.447744369506836, 70.72281837463379, 32.87482261657715, 33.385515213012695]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[69.87166404724121, 69.56768035888672, 77.12268829345703, 20.225048065185547, 21.230220794677734]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[70.5728530883789, 85.78824996948242, 94.4375991821289, 19.568443298339844, 90.30747413635254]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[72.48997688293457, 86.65966987609863, 94.43354606628418, 19.23203468322754, 91.13430976867676]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n",
      "[72.3724365234375, 85.78824996948242, 94.4375991821289, 19.576549530029297, 67.41547584533691]\n",
      "\n",
      "[0, 0, 0, 1]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python  \n",
    "# coding=utf-8  \n",
    "#本段代码实现树莓派智能小车的红外避障效果\n",
    "#代码使用的树莓派GPIO是用的BOARD编码方式。 \n",
    "import RPi.GPIO as GPIO\n",
    "\n",
    "#超声波避障\n",
    "from adafruit.Adafruit import Adafruit\n",
    "\n",
    "import time\n",
    "import sys\n",
    "\n",
    "#动作常量\n",
    "ACTION_UP = 0\n",
    "ACTION_DOWN = 1\n",
    "ACTION_LEFT = 2\n",
    "ACTION_RIGHT = 3\n",
    "\n",
    "data_file_path = \"ai/data/data.txt\"\n",
    "label_file_path = \"ai/data/label.txt\"\n",
    "\n",
    "#超声波\n",
    "adafruit = Adafruit()\n",
    "\n",
    "#采集距离\n",
    "def collect_dists(adafruit,is_opposite=False,step_angle=0.25):\n",
    "    dists = [0,0,0,0,0]\n",
    "    \n",
    "    #从右边开始采集\n",
    "    if is_opposite == False:\n",
    "        for i in range(5):\n",
    "            dists[i]=adafruit.angel_dist(step_angle*i)\n",
    "            # time.sleep(0.1)\n",
    "    #从左边开始采集\n",
    "    else:\n",
    "        for i in range(4,-1,-1):\n",
    "            dists[i]=adafruit.angel_dist(step_angle*i)\n",
    "            # time.sleep(0.1)\n",
    "    return dists\n",
    "       \n",
    "#写入数据\n",
    "def append_data(file_path,data):\n",
    "    with open(file_path, 'a') as file:\n",
    "        str_data = str(data)+\"\\n\"\n",
    "        file.write(str_data)\n",
    "        print(str_data)\n",
    "\n",
    "#创建标签数据\n",
    "def create_label(val):\n",
    "    data = [0,0,0,0]\n",
    "    data[val] = 1\n",
    "    return data\n",
    "    \n",
    "def setup():\n",
    "    GPIO.setwarnings(False)\n",
    "    GPIO.setmode(GPIO.BCM)       # Numbers GPIOs by physical location\n",
    "\n",
    "    #初始化超声波\n",
    "    adafruit.init()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    setup()\n",
    "    \n",
    "    is_opposite = False\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            #采集超声波距离\n",
    "            dists = collect_dists(adafruit,is_opposite)\n",
    "            #调转采集的方向\n",
    "            is_opposite = not is_opposite\n",
    "            \n",
    "            time.sleep(0.3)\n",
    "            \n",
    "            #采集输入数据\n",
    "            append_data(data_file_path,dists)\n",
    "            #采集标签数据\n",
    "            append_data(label_file_path,create_label(ACTION_RIGHT))\n",
    "            \n",
    "    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.\n",
    "        GPIO.cleanup()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76c250b6-3796-4821-a7dd-823bb081d2f0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c3cb94e-ab8f-419f-b6eb-49b647adc46b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow_env",
   "language": "python",
   "name": "tensorflow_env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
